/**
 * 
 * @authors Your Name (you@example.org)
 * @date    2018-06-06 21:25:22
 * @version $Id$
 */

// 兼容
window.requestAnimationFrame = window.requestAnimationFrame || function (callback){
	return setTimeout(callback,1000/60);
};
window.cancelAnimationFrame = window.cancelAnimationFrame || clearTimeout;

function animationTime(obj,attr,time,callback){
	var goTime = new Date(),
		startArr = {};

	for (var key in attr) {
		if(typeof attr[key] === "function"){
			startArr[key] = attr[key];
		}else{
			var start = parseFloat(getStyle(obj)[key]),
				change = parseFloat(attr[key]) - start;

			if(change !== 0){
				startArr[key] = {
					start : start,
					change : change
				};
			}
		}
	}

	(function fn(){
		var now = (new Date().getTime() - goTime)/time;
		if(now >= 1){
			now = 1;
			setStyle(obj,startArr,now);
		}else{
			setStyle(obj,startArr,now);
			requestAnimationFrame(fn);
		}
	})();

}

function setStyle(obj,startArr,now){
	for(var key in startArr) {
		if (key === 'opacity'){
			obj.style[key] = startArr[key].start + startArr[key].change*now;
		}else if(typeof startArr[key] === 'function'){
			startArr[key].call(obj,now,key);
		}else{
			obj.style[key] = startArr[key].start + startArr[key].change*now+"px";
		}
	}
}

function getStyle(obj){
	return obj.currentStyle || getComputedStyle(obj);
}